package com.jwaoo.mobile.http;

import com.dx.as.common.CommonUtils;
import com.dx.as.common.JWResult;
import com.dx.as.common.log.LogUtil;
import com.dx.as.common.string.StringUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jwaoo.mobile.view.account.AccountBean;
import com.jwaoo.mobile.view.explore.bean.UserBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import http.HttpClientHelper;
import http.IHttpListener;
import okhttp3.RequestBody;
import retrofit2.Call;

/**
 * Created by jwaoo on 2017/7/14.
 */

public class HttpHelper {

    private final static Gson gson = new Gson();
    private static IHttpServices http;
    private static IHttpServices getHttpSevices(){
        if (http == null){
            http = HttpClientHelper.getRetrofit().create(IHttpServices.class);
        }
        return http;
    }

    public static void doRegister(Map<String,Object> param,IHttpListener listener){
        String jsonStr = gson.toJson(param);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),jsonStr);
        Call call = getHttpSevices().postRegister(IHttpServices.register,body);
        HttpClientHelper.doNet(call,listener,new TypeToken<JWResult<Map<String,String>>>(){});
    }

    public static int doLogin(Map<String,Object> param,IHttpListener listener){
        String jsonStr = gson.toJson(param);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),jsonStr);
        Call call = getHttpSevices().postLogin(IHttpServices.login,body);
        HttpClientHelper.doNet(call,listener,new TypeToken<JWResult<Map<String,String>>>(){});
        return call.hashCode();
    }

    public static int getAccountInfo(IHttpListener listener){
        Call call = getHttpSevices().getAccountInfo(IHttpServices.account);
        HttpClientHelper.doNet(call,listener,new TypeToken<JWResult<AccountBean>>(){});
        return call.hashCode();
    }

    public static int getUrl(IHttpListener listener){
        Map<String ,Object> param = new HashMap<>(1);
        param.put("flag","all");
        String jsonStr = gson.toJson(param);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),jsonStr);
        Call call = getHttpSevices().postURI(IHttpServices.url,body);
        HttpClientHelper.doNet(call,listener,new TypeToken<JWResult<Map<String,Object>>>(){});
        return call.hashCode();
    }

    public static void getNearbyList(IHttpListener listener,Map map){
        String jsonStr = gson.toJson(map);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),jsonStr);
//        Call call = getHttpSevices().getAccountInfoList(IHttpServices.users,body);
        Call call = getHttpSevices().getAccountInfo(IHttpServices.users);
        HttpClientHelper.doNet(call,listener,new TypeToken<JWResult<List<UserBean>>>(){});
    }

    public static void getUserInfo(IHttpListener listener,int uuid){
        Call call = getHttpSevices().getUserInfo(IHttpServices.user+uuid);
        HttpClientHelper.doNet(call,listener,new TypeToken<JWResult<Map<String,Object>>>(){});
    }

    public static void postModify(Map<String,Object> param){
        String jsonStr = gson.toJson(param);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),jsonStr);
        Call call = getHttpSevices().postModify(IHttpServices.modify,body);
        HttpClientHelper.doNet(call, new IHttpListener() {
            @Override
            public void onResponse(Object response, int flg) {

            }

            @Override
            public void onFailure(Throwable t) {

            }
        }, new TypeToken<JWResult<Map<String, String>>>() {
        });
    }

    public static void doActive(){
        if (StringUtil.isEmpty(CommonUtils.getPushToken())) return;
        Map<String, Object> mapPush = new HashMap<>();
        mapPush.put("pushId", CommonUtils.getPushToken());
        LogUtil.d("HttpHelper","google token : "+CommonUtils.getPushToken());
        mapPush.put("longitude", "121.4179104567");
        mapPush.put("latitude", "31.1760313870");
        String jsonStr = gson.toJson(mapPush);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),jsonStr);
        Call call = getHttpSevices().postActive(IHttpServices.active,body);
        HttpClientHelper.doNet(call, new IHttpListener() {
            @Override
            public void onResponse(Object response, int flg) {

            }

            @Override
            public void onFailure(Throwable t) {
                doActive();
            }
        }, new TypeToken<JWResult<Object>>() {
        });
    }

    public static int doForgotPassword(Map<String,Object> param,IHttpListener listener){
        String jsonStr = gson.toJson(param);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),jsonStr);
        Call call = getHttpSevices().postForgotPassword(IHttpServices.forgot_password,body);
        HttpClientHelper.doNet(call,listener,new TypeToken<JWResult<Object>>(){});
        return call.hashCode();
    }

    public static int doUpdatePassword(Map<String,Object> param,IHttpListener listener){
        String jsonStr = gson.toJson(param);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),jsonStr);
        Call call = getHttpSevices().postPassword(IHttpServices.password,body);
        HttpClientHelper.doNet(call,listener,new TypeToken<JWResult<Object>>(){});
        return call.hashCode();
    }

    public static int doSendEmail(Map<String,Object> param,IHttpListener listener){
        String jsonStr = gson.toJson(param);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),jsonStr);
        Call call = getHttpSevices().postSendEmail(IHttpServices.email,body);
        HttpClientHelper.doNet(call,listener,new TypeToken<JWResult<Object>>(){});
        return call.hashCode();
    }


}
